package com.jymj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jymj.entity.KeyValueEntity;
import com.jymj.entity.SysUserEntity;
import com.jymj.entity.YlEntity;
import com.jymj.entity.dto.ApplyDto;
import com.jymj.entity.huji.ZhaiEntity;
import com.jymj.entity.zjd.ApplyLandEntity;
import com.jymj.entity.zjd.ApplyLandFile;
import com.jymj.service.ApplyLandService;
import com.jymj.service.YlService;
import com.jymj.service.ZhaiService;
import com.jymj.utils.FileUtile.FileUploadEntity;
import com.jymj.utils.FileUtile.FileUtile;
import com.jymj.utils.R;
import com.jymj.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${lc} on 2021/9/1.
 */
@RestController
@RequestMapping("/applyLand")
public class ApplyLandController {
    @Autowired
    private ApplyLandService applyLandService;
    @Autowired
    private YlService ylService;
    @Autowired
    private ZhaiService zhaiService;

    private final String APPLE_SAVE = "ApplyFile.dirPath";


    /**
     * 查询列表
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody ApplyDto applyDto){
        PageHelper.startPage(applyDto.getPage(),applyDto.getLimit());
        List<ApplyLandEntity> applyLandEntities = applyLandService.queryList(applyDto);
        PageInfo<ApplyLandEntity> applyLandEntityPageInfo = new PageInfo<>(applyLandEntities);
        return R.ok().put("data", applyLandEntityPageInfo);
    }

    /**
     * 查询详情
     * @return
     */
    @PostMapping("/info")
    public R info(@RequestBody ApplyDto applyDto){
        if (applyDto.getId() == null){
            return R.error("无参数");
        }
        List<ApplyLandEntity> applyLandEntity =  applyLandService.queryById(applyDto.getId());
        return R.ok().put("data",applyLandEntity);
    }

    /**
     * 点查询
     */
    @PostMapping("/getByPoint")
    public R getByPoint(@RequestBody ApplyDto applyDto){
        SysUserEntity user = ShiroUtils.getUserEntity();
        YlEntity ylEntity = ylService.getYLbyPoint(applyDto.getPoint(),user.getCode());
        if (ylEntity == null){
            return R.error("无院落信息");
        }
        List<ApplyLandEntity> applyLandEntitys = applyLandService.queryByYlId(ylEntity.getObjectid());

        if (applyLandEntitys != null && applyLandEntitys.size() > 0){
            for (ApplyLandEntity applyLandEntity : applyLandEntitys) {
                applyLandEntity.setYlArea(ylEntity.getArea());
                applyLandEntity.setYlJzmj(ylEntity.getJianzhuArea());
                applyLandEntity.setQszsh(ylEntity.getQszsh());
                applyLandEntity.setCzqk(ylEntity.getCzqk());
                applyLandEntity.setCzqkint(ylEntity.getCzqkint());
                applyLandEntity.setCenter(ylEntity.getCenter());
                applyLandEntity.setGeometry(ylEntity.getGeometry());
                applyLandEntity.setYlId(ylEntity.getObjectid());
                applyLandEntity.setYlEntity(ylEntity);
                applyLandEntity.setZhaiEntities(
                        zhaiService.getZhaiEntity(ylEntity.getObjectid(),applyLandEntity.getHousehold()));
                applyLandEntity.setZhaiEntity(
                        zhaiService.getzhai(ylEntity.getObjectid(),applyLandEntity.getHousehold()));
                applyLandEntity.setYlEntity(ylEntity);
            }
        }else {
            ApplyLandEntity applyLandEntity = new ApplyLandEntity();
            applyLandEntity.setYlEntity(ylEntity);
            List<ZhaiEntity> zhaiEntities = zhaiService.getZhaiEntity(ylEntity.getObjectid(),null);
            applyLandEntity.setZhaiEntities(zhaiEntities);
            ZhaiEntity zhaiEntity = zhaiService.getzhai(ylEntity.getObjectid(),null);
            applyLandEntity.setZhaiEntity(zhaiEntity);
            applyLandEntity.setYlArea(ylEntity.getArea());
            applyLandEntity.setYlJzmj(ylEntity.getJianzhuArea());
            applyLandEntity.setQszsh(ylEntity.getQszsh());
            applyLandEntity.setCzqk(ylEntity.getCzqk());
            applyLandEntity.setCzqkint(ylEntity.getCzqkint());
            applyLandEntity.setCenter(ylEntity.getCenter());
            applyLandEntity.setGeometry(ylEntity.getGeometry());
            applyLandEntitys.add(applyLandEntity);
        }

        return R.ok().put("data",applyLandEntitys);
    }

    @PostMapping("/getHuji")
    public R getHuji(@RequestBody ApplyDto applyDto){
        if (applyDto.getYlId() == null){
            return R.error("无参数");
        }
        List<ZhaiEntity> zhaiEntities = zhaiService.getHuji(applyDto.getYlId());
        return R.ok().put("data",zhaiEntities);
    }

    /**
     * 用地列表数量统计
     * @param applyDto
     * @return
     */
    @PostMapping("/queryStatistical")
    public R queryStatistical(@RequestBody ApplyDto applyDto){
        KeyValueEntity keyValueEntity = applyLandService.queryStatistical(applyDto);
        return R.ok().put("data",keyValueEntity);
    }

    /**
     * 用地汇总  按镇
     * @param applyDto
     * @return
     */
    @PostMapping("/getSummary")
    public R getSummary(@RequestBody ApplyDto applyDto){
        List<KeyValueEntity> keyValueEntities = applyLandService.getSummary(applyDto);
        return R.ok().put("data",keyValueEntities);
    }

    /**
     * 添加用地管理
     * @param applyLandEntitys
     * @return
     */
    @PostMapping("/saveLand")
    public R saveLand(@RequestBody List<ApplyLandEntity> applyLandEntitys){
        if (applyLandEntitys == null || applyLandEntitys.size() == 0){
            return R.error("无参数");
        }
        applyLandService.saveLand(applyLandEntitys);
        return R.ok("添加成功");
    }

    /**
     * 修改用地管理
     * @param applyLandEntitys
     * @return
     */
    @PostMapping("/updateLand")
    public R updateLand(@RequestBody List<ApplyLandEntity> applyLandEntitys){
        if (applyLandEntitys == null || applyLandEntitys.size() == 0){
            return R.error("无参数");
        }
        applyLandService.updateLand(applyLandEntitys);
        return R.ok("修改成功");
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Integer... ids){
        //判断是否有已通过的数据
        List<ApplyLandEntity> applyLandEntities = applyLandService.getAll(ids,1);
        if (applyLandEntities.size() > 0){
            return R.error("无法删除");
        }
        applyLandService.delete(ids);
        return R.ok("删除成功");
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("uploadFile")
    public R uploadFile(MultipartFile file, Integer cltype){
        FileUtile fileUtile = new FileUtile();
        FileUploadEntity fileUploadEntity = fileUtile.initFileUtile(APPLE_SAVE);
        fileUploadEntity= FileUtile.fileUpload(file, fileUploadEntity);
        ApplyLandFile applyLandFile = applyLandService.saveFile(fileUploadEntity, cltype);
        return R.ok().put("data",applyLandFile);
    }

    /**
     * 删除图片
     * @param ids
     * @return
     */
    @PostMapping("/deleteFile")
    public R deleteFile(@RequestBody Integer... ids){
        for(int i=0;i<ids.length;i++){
            ApplyLandFile applyLandFile = applyLandService.queryObject(ids[i]);
            FileUploadEntity fileUploadEntity = new FileUtile().initFileUtile(APPLE_SAVE);
            FileUtile.fileDelete(applyLandFile.getUrl(),fileUploadEntity);
            applyLandService.deleteFile(ids[i]);
        }
        return R.ok("删除成功！");
    }
}
